/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ui.views;

import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Component;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.LoginForm;
import com.vaadin.ui.LoginForm.LoginEvent;
import com.vaadin.ui.Panel;
import com.vaadin.ui.Window;
import com.vaadin.ui.Window.Notification;
import com.vaadin.ui.themes.Reindeer;
import ui.controllers.LoginController;

/**
 *
 * @author Spyros
 */
public class LoginView {

    private LoginForm loginForm;
    private Panel mainPanel;
    private LoginController loginController;
    private Integer userId;

    public LoginView() {
        loginController = new LoginController();
    }

    public Component init() {
        initializeComponents();
        return mainPanel;
    }

    private void initializeComponents() {
        mainPanel = new Panel();
        loginForm = new LoginForm();
        loginForm.setLoginButtonCaption("Connect");
        loginForm.addListener(new LoginForm.LoginListener() {

            @Override
            public void onLogin(LoginEvent event) {
                userId = loginController.login(
                        event.getLoginParameter("username"),
                        event.getLoginParameter("password"));
                if (userId != null) {
                    Window mainWindow = mainPanel.getWindow();
                    mainWindow.removeAllComponents();
                    Button logoutButton = new Button("Αποσύνδεση");
                    logoutButton.setStyleName(Reindeer.BUTTON_LINK);
                    logoutButton.addListener(new Button.ClickListener() {

                        @Override
                        public void buttonClick(ClickEvent event) {
                            Window mainWindow = event.getButton().getWindow();
                            mainWindow.removeAllComponents();
                            LoginView loginView = new LoginView();
                            mainWindow.addComponent(loginView.init());
                        }
                    });
                    HorizontalLayout horizontalLayout = new HorizontalLayout();
                    horizontalLayout.setSizeFull();
                    horizontalLayout.addComponent(logoutButton);
                    horizontalLayout.setComponentAlignment(logoutButton, Alignment.TOP_RIGHT);
                    mainWindow.addComponent(horizontalLayout);
                    FormView formView = new FormView();
                    mainWindow.addComponent(formView.init(userId));
                } else {
                    loginForm.getWindow().showNotification("Λάθος username ή password", Notification.TYPE_ERROR_MESSAGE);
                }
            }
        });
        mainPanel.addComponent(loginForm);
        Button registerButton = new Button("Εγγραφή");
        registerButton.setStyleName(Reindeer.BUTTON_LINK);
        registerButton.addListener(new Button.ClickListener() {

            @Override
            public void buttonClick(ClickEvent event) {
                Window mainWindow = event.getButton().getWindow();
                mainWindow.removeAllComponents();
                RegistrationView registrationView = new RegistrationView();
                mainWindow.addComponent(registrationView.init());
            }
        });
        mainPanel.addComponent(new Label("<br/><br/>", Label.CONTENT_XHTML));
        mainPanel.addComponent(registerButton);
    }
}
